package com.foreverwin.jzymes.eap.Listener;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.domain.AgvReqLog;
import com.foreverwin.jzymes.common.service.ISystemRuleAdviceService;
import com.foreverwin.jzymes.framework.annotation.MessageLog;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.cnc.service.ICncSfcPassAdviceService;
import org.apache.activemq.command.ActiveMQTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

@Component
public class TranshipListener {

    private Logger logger = LoggerFactory.getLogger( TranshipListener.class );


    @Autowired
    ICncSfcPassAdviceService cncSfcPassAdviceService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    JmsMessagingTemplate jmsTemplate;

    @JmsListener( destination = "MES_TRANSHIP_TRANSFER_INFRA" )
    @MessageLog
    public void sfcPassByTrayQtyFromQueue( String agvReqLogJson ) throws IOException {
        logger.info( "接收到接驳站Tray过站信息: " + agvReqLogJson );
        try{
            AgvReqLog agvReqLog = ObjectMapperUtil.readValueForUnderscore( agvReqLogJson, AgvReqLog.class );
            agvReqLog.setTranship( agvReqLog.getTranship().toUpperCase() );
            agvReqLog.setAgv( agvReqLog.getAgv().toUpperCase() );
            cncSfcPassAdviceService.sfcPassByTrayQty( agvReqLog );

            logger.info( "接驳站Tray过站信息处理完成: " + agvReqLogJson );
        }catch ( Exception e ){
            logger.error( "接驳站Tray过站信息处理失败: " + e.getMessage() + "，手工线接驳站Tray过站信息: " + agvReqLogJson );
            throw e;
        }
    }

    @JmsListener( destination = "MES_TRANSHIP_CURRENT_QTY" )
    @MessageLog
    public void publishBranchTranship( String branchTranshipJson ){
        logger.info( "接收到支线接驳站当前数量信息: " + branchTranshipJson );
        try {
            JSONObject map = JSONObject.parseObject( branchTranshipJson );

            Map<String,Object> transhipMap = new HashMap<>();
            transhipMap.put( "id", map.get( "TRANSHIP" ) );
            transhipMap.put( "rawNum", map.get( "RAW_NUM" ) );
            transhipMap.put( "clinkerNum", map.get( "CLINKER_NUM" ) );

            List<Map<String,Object>> ports = new ArrayList<>();
            ports.add( transhipMap );

            Map<String,Object> valueMap = new HashMap<>();
            valueMap.put( "date", DateUtil.format( new Date(), "yyyy-MM-dd" ) );
            valueMap.put( "time", DateUtil.format( new Date(), "HH:mm" ) );
            valueMap.put( "ports", ports );

            JSONObject eventData = new JSONObject();
            eventData.put( "CHANNEL", map.get( "CHANNEL" ) );
            eventData.put( "MESSAGE_TYPE", "WS_TRANSHIP_CURRENT_QTY" );
            eventData.put( "VALUE", valueMap );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "TRANSHIP_CURRENT_QTY" );
            appEvent.setData( eventData );
            ActiveMQTopic topic = new ActiveMQTopic( "MES_APP_EVENT_INFRA" );
            jmsTemplate.convertAndSend( topic, appEvent );


            logger.info( "支线接驳站当前数量信息处理完成: " + branchTranshipJson );
        } catch (Exception e) {
            logger.error( "支线接驳站当前数量信息处理失败: " + e.getMessage() + "，支线接驳站当前数量信息: " + branchTranshipJson );
            throw e;
        }
    }
}
